package com.kaiwukj.android.ufamily.mvp.ui.page.service.detail;

import android.app.Dialog;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.kaiwukj.android.mcas.utils.UToast;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ServiceResult;
import com.kaiwukj.android.ufamily.mvp.ui.widget.AmountJJLayout;
import com.kaiwukj.android.ufamily.mvp.ui.widget.FlowLayoutManager;
import com.kaiwukj.android.ufamily.mvp.ui.widget.TeamSpaceItemDecoration;

public class ServiceAppointmentDialog extends Dialog {

    private int checkPos = 0;

    public ServiceAppointmentDialog(@NonNull Context context, ServiceResult result) {
        super(context);
        init(context, result);
    }

    private void init(Context context, ServiceResult result) {
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.dialog_service_appointment);

        findViewById(R.id.tv_close).setOnClickListener(v -> this.cancel());

        ImageView ivImage = findViewById(R.id.iv_image);
        TextView tvName = findViewById(R.id.tv_name);
        TextView tvPrice = findViewById(R.id.tv_price);
        RecyclerView rvList = findViewById(R.id.rv_list);
        AmountJJLayout amountJJLayout = findViewById(R.id.container_amount);
        TextView tvSubmit = findViewById(R.id.tv_submit);


        if (result != null) {
            Glide.with(context)
                    .load(result.getThumbnail())
                    .into(ivImage);
            tvName.setText(result.getServiceName());
            ServiceAppointmentAdapter mAdapter = new ServiceAppointmentAdapter(result.getServiceParamList());
            rvList.setLayoutManager(new FlowLayoutManager());
            //rvList.setLayoutManager(new GridLayoutManager(context, 3));
            rvList.addItemDecoration(new TeamSpaceItemDecoration(SizeUtils.dp2px(3)));
            rvList.setAdapter(mAdapter);
            if (mAdapter.getItemCount() > 0) {
                mAdapter.checkItem(checkPos);
                tvPrice.setText(String.format("¥%s", mAdapter.getItem(checkPos).getCharges()));
            }
           /* mAdapter.setOnItemClickListener((adapter, view, position) -> {
                if (checkPos != position) {
                    amountJJLayout.setCount(1);
                }
                mAdapter.checkItem(position);
                checkPos = position;
                tvPrice.setText(String.format("¥%s", mAdapter.getItem(position).getCharges()));
            });*/

            mAdapter.setOnCheckChangedListener(position -> {
                if (checkPos != position) {
                    amountJJLayout.setCount(1);
                }
                // mAdapter.checkItem(position);
                checkPos = position;
                tvPrice.setText(String.format("¥%s", mAdapter.getItem(position).getCharges()));
            });

            amountJJLayout.setOnCountChangedListener(count -> {
                tvPrice.setText(String.format("¥%s", mAdapter.getItem(checkPos).getCharges() * count));
            });

            tvSubmit.setOnClickListener(v -> {
                if (mAdapter.getItemCount() > 0) {
                    this.cancel();
                    ARouter.getInstance().build(ARouterUrlKt.OrderCreateUrl)
                            .withSerializable("params", mAdapter.getCheckedItem())
                            .withInt("count", amountJJLayout.getCount())
                            .navigation();
                } else {
                    UToast.showShort(context.getApplicationContext(), "暂无服务项,请联系商家或客服");
                }
            });
        }
    }

}
